<template>
  <div class="for-present">
    <EditTable
      ref="editTable"
      @onChangeSelectAll="onSelectAll"
      @onJoinSale="onJoinSale"
      @onJoinSaleCheck="onJoinSaleCheck"
      :listData="columns"
      :type="type"
      @onDeleteSale="onDeleteSale"
      :list="$store.state.bargain.getBgUnLmtData"
    ></EditTable>
    <div class="pager">
      <Page 
        :total="totalItem" 
        :current="currentPage"
        :page-size="pageSize"
        @on-change="onPageChange"
        @on-page-size-change="onPageSizeChange"
        show-sizer 
        placement="top">
      </Page>
    </div>
    <Loader :status="status"></Loader>
  </div>
</template>
<script>
import EditTable from "./LimitBrandsTable";
import { columns } from "./LimitBrandsData";
import { deleteSale, setArrayValue, onJoinSaleCheck, getSelectStatus} from "@/views/sales/common/TableSaleMethods";
export default {
  name: "forpresent",
  components: {
    EditTable
  },
  data() {
    return {
      id: this.$route.query.id || '',
      columns,
      data1: [],
      type: '2',
      totalItem: null,
      pageSize: window.$pageSize,
      currentPage: 1,
      status: 'pass'
    };
  },
  mounted() {
    this.getSkuPage({size: window.$pageSize, current: 1, self: this})
  },
  methods: {
    onPageChange(val) {
      this.currentPage = Number(val)
      let param = { 
        size: this.pageSize,
        current: this.currentPage, 
        self: this
      }
      param.isLimitBrand = false
      param.activityId = this.id
      this.status = 'loading'
      this.$store.dispatch('getSkuPage', param).then(res => {
        this.$refs.editTable.selectAll = false
      })
    },
    onPageSizeChange(val) {
      this.pageSize = val
      let param = { 
        size: this.pageSize,
        current: this.currentPage, 
        self: this
      }
      param.isLimitBrand = false
      param.activityId = this.id
      this.status = 'loading'
      this.$store.dispatch('getSkuPage', param).then(res => {
        this.$refs.editTable.selectAll = false
      })
    },
    getSkuPage(param) {
      param.self = this
      param.isLimitBrand = false
      param.activityId = this.id
      this.status = 'loading'
      this.$store.dispatch('getSkuPage', param).then(data => {
        this.$refs.editTable.selectAll = false
        this.$store.dispatch("getSkuCount", {id: this.id, isLimitBrand: false})
      })
    },
    onSelectAll(param) {
      //点击选中所有checked按钮的时候出发的事件
      param
        ? setArrayValue(this.$store.state.bargain.getBgUnLmtData, {
            index: "checked",
            val1: true,
            childName: "productSkuList"
          })
        : setArrayValue(this.$store.state.bargain.getBgUnLmtData, {
            index: "checked",
            val1: false,
            childName: "productSkuList"
          });
    },
    onJoinSaleBody(item, todo) {
      if (item.productSkuList && item.productSkuList.length > 0) {
        item.checked
          ? setArrayValue(item.productSkuList, {
              index: "checked",
              val1: true,
              childName: "productSkuList"
            })
          : setArrayValue(item.productSkuList, {
              index: "checked",
              val1: false,
              childName: "productSkuList"
            });
      } else if (!item.productSkuList) {
        /* 当是子集的时候，选择一个对应的父级就选上 */
        let status = false;
        if (!todo) {
          return false;
        }
        for (let child of todo.productSkuList) {
          if (child.checked) {
            status = true;
          }
        }
        if (!status) {
          //子集没有被选中
          todo.checked = false;
        } else {
          todo.checked = true;
        }
      }
    },
    onJoinSaleCheck(item, todo) {
      // 点击checked按钮的时候触发的事件
      this.onJoinSaleBody(item, todo);
      // this.$refs.editTable.selectAll = getSelectStatus(
      //   this.$store.state.bargain.getBgUnLmtData,
      //   { index: "checked", childName: "productSkuList" }
      // );
    },
    onDeleteSale(index, data, index1, data1) {
      let ids = []
      if (index1 !== undefined) {
        ids = [data[index].acId]
      } else {
        ids = data[index].productSkuList.map(item=> {
          return item.acId
        })
      }
      this.status = "loading"
      this.$store.dispatch('deleteSku', {id: ids, activityId: this.id}).then(res => {
        this.status = "pass"
        this.$store.dispatch("getSkuCount", {id: this.id, isLimitBrand: false})
        this.$Notice.success({
          title: '温馨提示',
          desc: "删除商品成功"
        })
        deleteSale(index, data, index1, data1);
      })
    }
  }
};
</script>

